"""


f=open(fp,"r")
line=f.readline(2)
print("read:",line)

linecache.getline(‘文件名’，行)
f=open(fp,"w+")
s=input("输入:")
f.writelines(s)
f.close()"""


import os
fp="./temp/temp.txt"

if not os.path.exists("./temp"):
    os.makedirs("./temp")

import pygame
import time
import random
pygame.init()
screen=pygame.display.set_mode([640,550])
screen.fill([255,255,255])

f=open(fp,"r")
x=line = f.readline()
print(x)
x=int(x)
block_w=50
c_x=(block_w//2)+x
c_y=block_w//2
c_r=(block_w-6)//2

def boo(xx,yy):
    pygame.draw.rect(screen,[0,0,0],[xx,yy,25,25],0)
    pygame.draw.rect(screen,[255,0,0],[xx+25,yy,25,25],0)
    pygame.draw.rect(screen,[0,255,0],[xx,yy+25,25,25],0)
    pygame.draw.rect(screen,[0,0,255],[xx+25,yy+25,25,25],0)
N=10
i=0
while i<N:
    j=0
    while j<N:
        pygame.draw.rect(screen,[0,255,0],[i*block_w,j*block_w,block_w,block_w],2)
        boo(i*block_w,j*block_w)
        j+=1
    i+=1

pygame.draw.circle(screen, [255,0,0], [c_x,c_y], c_r)
pygame.display.flip()
def is_out_range(c_x,c_y):
    if c_x<=0 or c_x>=block_w*10:
        return True
    if c_y<=0 or c_y>=block_w*10:
        return True
    return False

running=True
while running:
    for event in pygame.event.get():
        # print(event)
        if event.type == pygame.QUIT:
            running=False
        elif event.type == pygame.KEYDOWN:
            c_x_old=c_x
            c_y_old=c_y
            # print(event.key)
            if event.key == pygame.K_UP:
                print("向上")
                if not is_out_range(c_x,c_y-1*block_w):                            
                    c_y-=block_w
            elif event.key == pygame.K_DOWN:
                print("向下")
                if not is_out_range(c_x,c_y+1*block_w):                            
                    c_y+=1*block_w
            elif event.key == pygame.K_LEFT:
                print("向左")
                if not is_out_range(c_x-1*block_w,c_y):                            
                    c_x-=1*block_w
            elif event.key == pygame.K_RIGHT:
                print("向右")
                if not is_out_range(c_x+1*block_w,c_y):                            
                    c_x+=1*block_w
            pygame.draw.circle(screen, [255,255,255], [c_x_old,c_y_old], c_r)
            pygame.draw.circle(screen, [255,0,0], [c_x,c_y], c_r)
            boo(c_x_old-25,c_y_old-25)
            pygame.display.flip()
x=int(c_x)
f=open(fp,"w+")
line=write(str(x))
f.close()
pygame.quit()